/**
 * 
 */
package org.vsg.stock.presentation;

import java.net.URLDecoder;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.vsg.common.web.json.ListResult;
import org.vsg.common.web.json.SubmitResult;
import org.vsg.common.web.json.processor.JsDateViewJsonValueProcessor;
import org.vsg.stock.basic.domain.StockCategory;
import org.vsg.stock.extension.service.StockAnalysisService;
import org.vsg.stock.extension.vo.AttentionBlock;
import org.vsg.stock.extension.vo.LinkageCateShownSequenceVO;
import org.vsg.stock.extension.vo.LinkageCateStockVO;
import org.vsg.stock.extension.vo.LinkageStockGroupCounterView;

/**
 * @author vison
 *
 */
@Controller
public class BlockController {

	private final static Logger logger = LoggerFactory.getLogger(BlockController.class);	

	private final static String MODULE_NAME = "stock";
	
	private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	
	@Resource
	private StockAnalysisService stockAnalysisService;
	
	@RequestMapping(value="/"+MODULE_NAME+"/stockAssemblyConcept.do")	
	public ModelAndView stockAssemblyConcept(HttpServletRequest request, HttpServletResponse response) throws Exception {
		

		
		/**
		 * defined the module and view handle
		 */
		ModelAndView mav = new ModelAndView();
        mav.setViewName("/stock/stock-assembly-concept");	// view name
        // --- convert model to data format
		JsonConfig jcfg = new JsonConfig();
        return mav;		
    }

	@RequestMapping(value="/"+MODULE_NAME+"/stockAssemblyPlate.do")	
	public ModelAndView stockAssemblyPlate(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		/**
		 * defined the module and view handle
		 */
		ModelAndView mav = new ModelAndView();
        mav.setViewName("/stock/stock-assembly-plate");	// view name
        // --- convert model to data format
		JsonConfig jcfg = new JsonConfig();

        return mav;		
    }

	
	@RequestMapping(value="/"+MODULE_NAME+"/stockAssemblyTrades.do")	
	public ModelAndView stockAssemblyTrades(HttpServletRequest request, HttpServletResponse response) throws Exception {
		

		
		/**
		 * defined the module and view handle
		 */
		ModelAndView mav = new ModelAndView();
        mav.setViewName("/stock/stock-assembly-trades");	// view name
        return mav;		
    }

	@RequestMapping(value="/"+MODULE_NAME+"/stockAssemblySameStockHolder.do")	
	public ModelAndView stockAssemblySameStockHolder(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		/**
		 * defined the module and view handle
		 */
		ModelAndView mav = new ModelAndView();
        mav.setViewName("/stock/stock-assembly-sameStockHolder");	// view name
        return mav;		
    }

	@RequestMapping(value="/"+MODULE_NAME+"/stockAssemblyZone.do")	
	public ModelAndView stockAssemblyZone(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		/**
		 * defined the module and view handle
		 */
		ModelAndView mav = new ModelAndView();
        mav.setViewName("/stock/stock-assembly-zone");	// view name
        return mav;		
    }	
	
	@RequestMapping(value="/"+MODULE_NAME+"/stockAssemblyStandardTrades.do")	
	public ModelAndView stockAssemblyStandardTrades(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		/**
		 * defined the module and view handle
		 */
		ModelAndView mav = new ModelAndView();
        mav.setViewName("/stock/stock-assembly-standardTrades");	// view name
        return mav;		
    }	

	
	@RequestMapping(value="/"+MODULE_NAME+"/stockAssemblyOthers.do")	
	public ModelAndView stockAssemblyOthers(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		/**
		 * defined the module and view handle
		 */
		ModelAndView mav = new ModelAndView();
        mav.setViewName("/stock/stock-assembly-others");	// view name
        return mav;		
    }

	@RequestMapping(value="/"+MODULE_NAME+"/assemblyMain.do")	
	public ModelAndView assemblyMain() throws Exception {
		
		/**
		 * defined the module and view handle
		 */
		ModelAndView mav = new ModelAndView();
        mav.setViewName("/stock/assembly-main");	// view name
        return mav;		
    }	

	@RequestMapping(value="/"+MODULE_NAME+"/attentionBlockStockMap.do")	
	public ModelAndView attentionBlockStockMap() throws Exception {
		
		/**
		 * defined the module and view handle
		 */
		ModelAndView mav = new ModelAndView();
        mav.setViewName("/stock/block_atten_stock");	// view name
        // --- convert model to data format
		JsonConfig jcfg = new JsonConfig();

        return mav;		
    }
	
	@RequestMapping(value="/"+MODULE_NAME+"/attentionBlocks.do")	
	public ModelAndView attentionBlocks(HttpServletRequest request) throws Exception {
		HttpSession sess = request.getSession();		
		List<AttentionBlock> blockList = (List)sess.getAttribute( "attentionBlockList" );
		blockList = blockList == null ? new Vector(): blockList;

		// ----- add map to json object ----
		ListResult lr = new ListResult();
		lr.setResults( blockList.size() );
		lr.setRows(blockList);		
		
		/**
		 * defined the module and view handle
		 */
		/**
		 * defined the module and view handle
		 */
		ModelAndView mav = new ModelAndView();
        mav.setViewName("json");	// view name
        // --- convert model to data format
		JsonConfig jcfg = new JsonConfig();
		jcfg.registerJsonValueProcessor(java.util.Date.class,new JsDateViewJsonValueProcessor());
		JSONObject jsonObj = JSONObject.fromObject(lr, jcfg);        
        mav.addObject("json",jsonObj);
        
        logger.info(jsonObj.toString());        
        return mav;	
    }
	
	
	
	@RequestMapping(value="/"+MODULE_NAME+"/stockAssemblyTopAction.do")	
	public ModelAndView stockAssemblyTopAction(int top , String type , HttpServletRequest request, HttpServletResponse response) throws Exception {
		String cateName = "";
		if (StockCategory.CONCEPT.equalsIgnoreCase( type )) {
			cateName = StockCategory.CONCEPT;
		}
		else if ( StockCategory.PLATE.equalsIgnoreCase( type ) ) {
			cateName = StockCategory.PLATE;
		}
		else if ( StockCategory.TRADES.equalsIgnoreCase( type ) ) {
			cateName = StockCategory.TRADES;
		}
		else if ( StockCategory.SAME_STOCKHOLDER.equalsIgnoreCase( type ) ) {
			cateName = StockCategory.SAME_STOCKHOLDER;
		}		
		else if ( StockCategory.STANDARD_TRADES.equalsIgnoreCase( type ) ) {
			cateName = StockCategory.STANDARD_TRADES;
		}		
		else if ( StockCategory.ZONE.equalsIgnoreCase( type ) ) {
			cateName = StockCategory.ZONE;
		}
		else if ( StockCategory.OTHERS.equalsIgnoreCase( type ) ) {
			cateName = StockCategory.OTHERS;
		}
		
		// --- get the date time ---
		Calendar cale = Calendar.getInstance();
		Date fromDate = cale.getTime();
		
		List<LinkageStockGroupCounterView> viewList = this.stockAnalysisService.findLinkageStockGroupCounterViewInFrontOf(fromDate, top, cateName);
		
		// ----- add map to json object ----
		ListResult lr = new ListResult();
		lr.setResults( viewList.size() );
		lr.setRows(viewList);
		
		/**
		 * defined the module and view handle
		 */
		ModelAndView mav = new ModelAndView();
        mav.setViewName("json");	// view name
        // --- convert model to data format
		JsonConfig jcfg = new JsonConfig();
		//jcfg.registerJsonValueProcessor(java.sql.Timestamp.class,new JsTimestampJsonValueProcessor());
		JSONObject jsonObj = JSONObject.fromObject(lr, jcfg);        
        mav.addObject("json",jsonObj);
        
        logger.info(jsonObj.toString());        
        return mav;		
    }
	
	@RequestMapping(value="/"+MODULE_NAME+"/stockAssemblyNewTopAction.do")	
	public ModelAndView stockAssemblyNewTopAction(int notTop,int top , String type , HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String cateName = "";
		if (StockCategory.CONCEPT.equalsIgnoreCase( type )) {
			cateName = StockCategory.CONCEPT;
		}
		else if ( StockCategory.PLATE.equalsIgnoreCase( type ) ) {
			cateName = StockCategory.PLATE;
		}
		else if ( StockCategory.TRADES.equalsIgnoreCase( type ) ) {
			cateName = StockCategory.TRADES;
		}
		else if ( StockCategory.SAME_STOCKHOLDER.equalsIgnoreCase( type ) ) {
			cateName = StockCategory.SAME_STOCKHOLDER;
		}		
		else if ( StockCategory.STANDARD_TRADES.equalsIgnoreCase( type ) ) {
			cateName = StockCategory.STANDARD_TRADES;
		}		
		else if ( StockCategory.ZONE.equalsIgnoreCase( type ) ) {
			cateName = StockCategory.ZONE;
		}
		else if ( StockCategory.OTHERS.equalsIgnoreCase( type ) ) {
			cateName = StockCategory.OTHERS;
		}		
		// --- get the date time ---
		Calendar cale = Calendar.getInstance();
		Date fromDate = cale.getTime();
		
		List<LinkageStockGroupCounterView> viewList = this.stockAnalysisService.findLinkageStockGroupCounterViewInFrontOf(fromDate, top,notTop, cateName);
		

		// ----- add map to json object ----
		ListResult lr = new ListResult();
		lr.setResults(viewList.size());
		lr.setRows(viewList);
		
		/**
		 * defined the module and view handle
		 */
		ModelAndView mav = new ModelAndView();
        mav.setViewName("json");	// view name
        // --- convert model to data format
		JsonConfig jcfg = new JsonConfig();
		//jcfg.registerJsonValueProcessor(java.sql.Timestamp.class,new JsTimestampJsonValueProcessor());
		JSONObject jsonObj = JSONObject.fromObject(lr, jcfg);        
        mav.addObject("json",jsonObj);
        
        logger.info(jsonObj.toString());        
        return mav;		
    }

	
	
	@RequestMapping(value="/"+MODULE_NAME+"/stockAssembly.do")		
	public ModelAndView stockAssembly(HttpServletRequest request, HttpServletResponse response) throws Exception {

		/**
		 * defined the module and view handle
		 */
		ModelAndView mav = new ModelAndView();
        mav.setViewName("/stock/stock-assembly");	// view name
        // --- convert model to data format
		JsonConfig jcfg = new JsonConfig();
        return mav;		
	}

	
	@RequestMapping(value="/"+MODULE_NAME+"/stockAssemblyCharts.do")		
	public ModelAndView stockAssemblyCharts(HttpServletRequest request, HttpServletResponse response) throws Exception {

		/**
		 * defined the module and view handle
		 */
		ModelAndView mav = new ModelAndView();
        mav.setViewName("/stock/stock-assembly-charts");	// view name
        // --- convert model to data format
		JsonConfig jcfg = new JsonConfig();
        return mav;		
	}

	@RequestMapping(value="/"+MODULE_NAME+"/stockAssemblyFrequency.do")		
	public ModelAndView stockAssemblyFrequency(HttpServletRequest request, HttpServletResponse response) throws Exception {

		/**
		 * defined the module and view handle
		 */
		ModelAndView mav = new ModelAndView();
        mav.setViewName("/stock/stock-assembly-frequency");	// view name
        // --- convert model to data format
		JsonConfig jcfg = new JsonConfig();
        return mav;		
	}
	
	
	@RequestMapping(value="/"+MODULE_NAME+"/{refDate}/{cycle}/{chartsCycle}/stockAssemblyChartsAction.do" )	
	public ModelAndView stockAssemblyChartsAction(
			@PathVariable String refDate,
			@PathVariable int cycle,
			@PathVariable int chartsCycle 
			) throws Exception {
	
		Date fromDate = dateFormat.parse( refDate );


		List<LinkageStockGroupCounterView> viewList = 
			stockAnalysisService.findAllLinkageStockGroupCounterViewInFrontOf(fromDate, cycle, chartsCycle);


		// ----- add map to json object ----
		ListResult lr = new ListResult();
		lr.setResults(viewList.size());
		lr.setRows(viewList);
		
		/**
		 * defined the module and view handle
		 */
		ModelAndView mav = new ModelAndView();
        mav.setViewName("json");	// view name
        // --- convert model to data format
		JsonConfig jcfg = new JsonConfig();
		//jcfg.registerJsonValueProcessor(java.sql.Timestamp.class,new JsTimestampJsonValueProcessor());
		JSONObject jsonObj = JSONObject.fromObject(lr, jcfg);        
        mav.addObject("json",jsonObj);
        
        logger.info(jsonObj.toString());        
        return mav;		
    }	


	@RequestMapping(value="/"+MODULE_NAME+"/{refDate}/stockAssemblyChartsAction.do" )	
	public ModelAndView stockTodayAssemblyChartsAction(
			@PathVariable String refDate
			) throws Exception {
	
		Date fromDate = dateFormat.parse( refDate );


		List<LinkageStockGroupCounterView> viewList = 
			stockAnalysisService.findAllLinkageStockGroupCounterViewInFrontOf(fromDate);


		// ----- add map to json object ----
		ListResult lr = new ListResult();
		lr.setResults(viewList.size());
		lr.setRows(viewList);
		
		/**
		 * defined the module and view handle
		 */
		ModelAndView mav = new ModelAndView();
        mav.setViewName("json");	// view name
        // --- convert model to data format
		JsonConfig jcfg = new JsonConfig();
		//jcfg.registerJsonValueProcessor(java.sql.Timestamp.class,new JsTimestampJsonValueProcessor());
		JSONObject jsonObj = JSONObject.fromObject(lr, jcfg);        
        mav.addObject("json",jsonObj);
        
        logger.info(jsonObj.toString());        
        return mav;		
    }	

	
	@RequestMapping(value="/"+MODULE_NAME+"/{refDate}/{cycle}/stockChartsDateShownAction.do" )	
	public ModelAndView stockChartsDateShownAction(
			@PathVariable String refDate,
			@PathVariable int cycle,
			String cateDesc 
			) throws Exception {
	
		Date fromDate = dateFormat.parse( refDate );
		
		cateDesc = URLDecoder.decode( cateDesc ,"UTF-8");

		List<LinkageCateShownSequenceVO> viewList =
			stockAnalysisService.findCateShownSequenceByCategoryDesc(fromDate, cycle, cateDesc);


		// ----- add map to json object ----
		ListResult lr = new ListResult();
		lr.setResults(viewList.size());
		lr.setRows(viewList);
		
		/**
		 * defined the module and view handle
		 */
		ModelAndView mav = new ModelAndView();
        mav.setViewName("json");	// view name
        // --- convert model to data format
		JsonConfig jcfg = new JsonConfig();
		jcfg.registerJsonValueProcessor(java.util.Date.class,new JsDateViewJsonValueProcessor());
		JSONObject jsonObj = JSONObject.fromObject(lr, jcfg);        
        mav.addObject("json",jsonObj);
        
        logger.info(jsonObj.toString());        
        return mav;		
    }	
	
	@RequestMapping(value="/"+MODULE_NAME+"/{refDate}/{cycle}/stockChartsListAction.do" )	
	public ModelAndView stockChartsListAction(
			@PathVariable String refDate,
			@PathVariable int cycle,
			String cateDesc 
			) throws Exception {
	
		Date fromDate = dateFormat.parse( refDate );
		
		cateDesc = URLDecoder.decode( cateDesc ,"UTF-8");
		
		List<LinkageCateStockVO> viewList = stockAnalysisService.findCateStockByCategoryDesc(fromDate, cycle, cateDesc);


		// ----- add map to json object ----
		ListResult lr = new ListResult();
		lr.setResults(viewList.size());
		lr.setRows(viewList);
		
		/**
		 * defined the module and view handle
		 */
		ModelAndView mav = new ModelAndView();
        mav.setViewName("json");	// view name
        // --- convert model to data format
		JsonConfig jcfg = new JsonConfig();
		JSONObject jsonObj = JSONObject.fromObject(lr, jcfg);        
        mav.addObject("json",jsonObj);
        
        logger.info(jsonObj.toString());        
        return mav;		
    }		


	@RequestMapping(value="/"+MODULE_NAME+"/{refDate}/{cycle}/stockAssemblyFrequencyAction.do" )	
	public ModelAndView stockAssemblyFrequencyAction(
			@PathVariable String refDate,
			@PathVariable int cycle
			) throws Exception {
	
		Date fromDate = dateFormat.parse( refDate );


		List<LinkageStockGroupCounterView> viewList = 
			stockAnalysisService.findFrequencyLinkageStockGroup(fromDate, cycle);


		// ----- add map to json object ----
		ListResult lr = new ListResult();
		lr.setResults(viewList.size());
		lr.setRows(viewList);
		
		/**
		 * defined the module and view handle
		 */
		ModelAndView mav = new ModelAndView();
        mav.setViewName("json");	// view name
        // --- convert model to data format
		JsonConfig jcfg = new JsonConfig();
		//jcfg.registerJsonValueProcessor(java.sql.Timestamp.class,new JsTimestampJsonValueProcessor());
		JSONObject jsonObj = JSONObject.fromObject(lr, jcfg);        
        mav.addObject("json",jsonObj);
        
        logger.info(jsonObj.toString());        
        return mav;		
    }	

	
	@RequestMapping(value="/"+MODULE_NAME+"/{refDate}/{cycle}/{recent}/stockAssemblyFrequencyAction.do" )	
	public ModelAndView stockAssemblyRecentFrequencyAction(
			@PathVariable String refDate,
			@PathVariable int cycle,
			@PathVariable int recent
			) throws Exception {
	
		Date fromDate = dateFormat.parse( refDate );


		List<LinkageStockGroupCounterView> viewList = 
			stockAnalysisService.findFrequencyLinkageStockGroup(fromDate, cycle , recent);


		// ----- add map to json object ----
		ListResult lr = new ListResult();
		lr.setResults(viewList.size());
		lr.setRows(viewList);
		
		/**
		 * defined the module and view handle
		 */
		ModelAndView mav = new ModelAndView();
        mav.setViewName("json");	// view name
        // --- convert model to data format
		JsonConfig jcfg = new JsonConfig();
		//jcfg.registerJsonValueProcessor(java.sql.Timestamp.class,new JsTimestampJsonValueProcessor());
		JSONObject jsonObj = JSONObject.fromObject(lr, jcfg);        
        mav.addObject("json",jsonObj);
        
        logger.info(jsonObj.toString());        
        return mav;		
    }	
	
	
	@RequestMapping(value="/"+MODULE_NAME+"/{refDate}/{cycle}/stockFrequencyDateShownAction.do" )	
	public ModelAndView stockFrequencyDateShownAction(
			@PathVariable String refDate,
			@PathVariable int cycle,
			String cateDesc 
			) throws Exception {
	
		Date fromDate = dateFormat.parse( refDate );
		
		cateDesc = URLDecoder.decode( cateDesc ,"UTF-8");

		List<LinkageCateShownSequenceVO> viewList =
			stockAnalysisService.findCateShownSequenceByCategoryDesc(fromDate, cycle, cateDesc);


		// ----- add map to json object ----
		ListResult lr = new ListResult();
		lr.setResults(viewList.size());
		lr.setRows(viewList);
		
		/**
		 * defined the module and view handle
		 */
		ModelAndView mav = new ModelAndView();
        mav.setViewName("json");	// view name
        // --- convert model to data format
		JsonConfig jcfg = new JsonConfig();
		jcfg.registerJsonValueProcessor(java.util.Date.class,new JsDateViewJsonValueProcessor());
		JSONObject jsonObj = JSONObject.fromObject(lr, jcfg);        
        mav.addObject("json",jsonObj);
        
        logger.info(jsonObj.toString());        
        return mav;		
    }	

	@RequestMapping(value="/"+MODULE_NAME+"/{refDate}/{cycle}/stockFrequencyListAction.do" )	
	public ModelAndView stockFrequencyListAction(
			@PathVariable String refDate,
			@PathVariable int cycle,
			String cateDesc 
			) throws Exception {
	
		Date fromDate = dateFormat.parse( refDate );
		
		cateDesc = URLDecoder.decode( cateDesc ,"UTF-8");
		
		List<LinkageCateStockVO> viewList = stockAnalysisService.findCateStockByCategoryDesc(fromDate, cycle, cateDesc);


		// ----- add map to json object ----
		ListResult lr = new ListResult();
		lr.setResults(viewList.size());
		lr.setRows(viewList);
		
		/**
		 * defined the module and view handle
		 */
		ModelAndView mav = new ModelAndView();
        mav.setViewName("json");	// view name
        // --- convert model to data format
		JsonConfig jcfg = new JsonConfig();
		JSONObject jsonObj = JSONObject.fromObject(lr, jcfg);        
        mav.addObject("json",jsonObj);
        
        logger.info(jsonObj.toString());        
        return mav;		
    }		

	@RequestMapping(value="/"+MODULE_NAME+"/saveOwnResearchBlock.do" )	
	public ModelAndView saveOwnResearchBlockAction(
			String records , HttpServletRequest request) throws Exception {
		HttpSession sess = request.getSession();
	
		List<AttentionBlock> blockList = (List)sess.getAttribute( "attentionBlockList" );
		if (blockList == null) {
			blockList = new Vector<AttentionBlock>();
		}
		logger.info( records );
		
		JSONArray jsonArray = JSONArray.fromObject( records ); 
		
		AttentionBlock blockView = null;
		for (int i = 0  ; i < jsonArray.size(); i++) {
			
			JSONObject jo = (JSONObject)jsonArray.get(i);

			// --- check the session handle ---
			blockView = new AttentionBlock();
			blockView.setId( jo.getString("id") );
			blockView.setType( jo.getString("type") );
			blockView.setDescription( jo.getString("description") );
			blockView.setCycle( Integer.parseInt( jo.getString("cycle") )  );
			blockView.setStartDate( dateFormat.parse( jo.getString("startDate") ) );
			
			stockAnalysisService.saveAttentionBlock(blockList, blockView);
		}
		

		SubmitResult submitResult = new SubmitResult();
		submitResult.setSuccess(true);
		submitResult.setMsg("成功添加关注板块!" );
		
		/**
		 * defined the module and view handle
		 */
		ModelAndView mav = new ModelAndView();
        mav.setViewName("result");	// view name
        // --- convert model to data format
		JSONObject jsonObj = JSONObject.fromObject(submitResult );         
        mav.addObject("json",jsonObj);
        
        sess.setAttribute("attentionBlockList", blockList);

        logger.info(jsonObj.toString());        
        return mav;		
    }	

	
	@RequestMapping(value="/"+MODULE_NAME+"/removeOwnResearchBlock.do" )	
	public ModelAndView removeOwnResearchBlockAction(
			String records , HttpServletRequest request) throws Exception {
		HttpSession sess = request.getSession();
	
		List<AttentionBlock> blockList = (List)sess.getAttribute( "attentionBlockList" );
		if (blockList == null) {
			blockList = new Vector<AttentionBlock>();
		}
		logger.info( records );
		
		String[] ids = records.split(",");

		List<String> idsList = Arrays.asList(ids);

		AttentionBlock ab = null;
		for (Iterator<AttentionBlock> blockIter = blockList.iterator() ; blockIter.hasNext(); ) {
			ab = blockIter.next();
			if (idsList.contains( ab.getId() )) {
				blockIter.remove();
			}
		}

		

		SubmitResult submitResult = new SubmitResult();
		submitResult.setSuccess(true);
		submitResult.setMsg("成功移除关注板块!" );
		
		/**
		 * defined the module and view handle
		 */
		ModelAndView mav = new ModelAndView();
        mav.setViewName("result");	// view name
        // --- convert model to data format
		JSONObject jsonObj = JSONObject.fromObject(submitResult );         
        mav.addObject("json",jsonObj);
        
        sess.setAttribute("attentionBlockList", blockList);

        logger.info(jsonObj.toString());        
        return mav;		
    }	
	
	
	@RequestMapping(value="/"+MODULE_NAME+"/attentionBlocksAnalysisStocks.do" )	
	public ModelAndView attentionBlocksAnalysisStocks(
			String blockIds , HttpServletRequest request) throws Exception {
		HttpSession sess = request.getSession();
	
		List<AttentionBlock> blockList = (List)sess.getAttribute( "attentionBlockList" );
		if (blockList == null) {
			blockList = new Vector<AttentionBlock>();
		}
		
		// --- filter boject handle ---
		String[] bIds = blockIds.split(",");
		List<String> idsArray = Arrays.asList(bIds);
		
		List<AttentionBlock> selAttentionBlocks = new Vector<AttentionBlock>();
		
		AttentionBlock  ab = null;
		for (Iterator<AttentionBlock> blockIter = blockList.iterator() ; blockIter.hasNext(); ) {
			ab = blockIter.next();
			
			if (idsArray.contains( ab.getId() ) ) {
				selAttentionBlocks.add( ab );
			}
		}
		// --- fine the block object ---
		List<LinkageCateStockVO> viewList =  stockAnalysisService.analyseStocksFromAttentionBlock( selAttentionBlocks );

		
		// ----- add map to json object ----
		ListResult lr = new ListResult();
		lr.setResults(viewList.size());
		lr.setRows(viewList);
		
		/**
		 * defined the module and view handle
		 */
		ModelAndView mav = new ModelAndView();
        mav.setViewName("json");	// view name
        // --- convert model to data format
		JsonConfig jcfg = new JsonConfig();
		jcfg.registerJsonValueProcessor(java.util.Date.class,new JsDateViewJsonValueProcessor());
		JSONObject jsonObj = JSONObject.fromObject(lr, jcfg);        
        mav.addObject("json",jsonObj);
        
        logger.info(jsonObj.toString());        
        return mav;
    }	
	
}
